﻿using Gobang.Client.Common;
using Gobang.ToolClass.Model;
using Sunny.UI;
using System.Net;

namespace Gobang.Client.View
{
    public partial class FrmRegister : UIForm
    {
        public FrmRegister()
        {
            InitializeComponent();
            uiGroupBox2.Hide();
            NetworkingCenter.RegisteredAction = Registered;
        }

        private void sbtnRegister_Click(object sender, EventArgs e)
        {
            string userName = this.txtUserName.Text.Trim();
            if (string.IsNullOrWhiteSpace(userName))
            {
                this.ShowWarningNotifier("请输入用户名");
                return;
            }
            string pwd = this.txtPwd.Text.Trim();
            if (string.IsNullOrWhiteSpace(pwd))
            {
                this.ShowWarningNotifier("请输入密码");
                return;
            }
            if (pwd.Length < 6)
            {
                this.ShowWarningNotifier("密码长度不能小于6");
                return;
            }
            string pwd2 = this.txtAginPwd.Text.Trim();
            if (string.IsNullOrWhiteSpace(pwd2))
            {
                this.ShowWarningNotifier("请再次输入密码");
                return;
            }
            if (!pwd.Equals(pwd2))
            {
                this.ShowWarningNotifier("两次密码不一致，请检查");
                return;
            }

            this.ShowWaitForm();

            MsgMergeModel msgMergeModel = new MsgMergeModel();
            UserInfoModel userInfoModel = new UserInfoModel
            {
                Name = userName,
                Password = pwd,
                Sex = rbtnMan.Checked ? "男" : "女",
            };
            msgMergeModel.UserInfo = userInfoModel;
            msgMergeModel.SendMsgType = ToolClass.MsgType.Command;
            msgMergeModel.SendMsgCommand = ToolClass.MsgCommand.Registering;
            NetworkingCenter.SendToServer(msgMergeModel.GetBytes());
        }
        private void Registered(MsgMergeModel msgMerge, IPEndPoint iPEndPoint)
        {
            Thread.Sleep(500);
            if (msgMerge.SendMsgCommand == ToolClass.MsgCommand.Registered)
            {
                this.ExecBeginInvoke(() =>
                {
                    this.ShowSuccessNotifier("注册成功");
                    this.uiGroupBox2.Show();
                    this.uiLabel1.Text = msgMerge.UserInfo.Accountnumber;
                    this.HideWaitForm();
                });
            }
            else
            {
                this.ExecBeginInvoke(() =>
                {
                    this.ShowErrorNotifier("注册失败");
                    this.HideWaitForm();
                });
            }
        }
        private void FrmRegister_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
        }
        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbtnCopy_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(this.uiLabel1.Text);
            this.ShowSuccessNotifier("复制成功");
        }
    }
}
